﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Net;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Animation;
using System.Windows.Shapes;
using System.Windows.Navigation;

using ObjModel = System.Collections.ObjectModel;
using System.Windows.Data;
using System.ComponentModel;

namespace OrgStructure
{
    public partial class Page1 : Page
    {

        #region Members
        public ObjModel.ObservableCollection<SPUser> _SPUSersList_Tmp;
        public ObjModel.ObservableCollection<SPUser> _SPUSersList;
        PagedCollectionView myListView;
        string FindTextStr = "";
        private ChildWindow1 oDlg = new ChildWindow1();
        #endregion

        #region Constructors

        public Page1()
        {
            InitializeComponent();
        }

        #endregion

        #region Common Methods
        private void ApplyGridStyles()
        {
            if (myListView.CanGroup == true)
            {
                myListView.GroupDescriptions.Add(new PropertyGroupDescription("Офис"));
                myListView.GroupDescriptions.Add(new PropertyGroupDescription("Отдел"));
                myListView.GroupDescriptions.Add(new PropertyGroupDescription("Должность"));
            }
            if (myListView.CanSort == true)
            {
                myListView.SortDescriptions.Add(new SortDescription("Отдел", ListSortDirection.Ascending));
            }
        }
        #endregion

        #region Events
        protected override void OnNavigatedTo(NavigationEventArgs e)
        {
            Invalidate();
        }
        private void Button_Click_Expand(object sender, RoutedEventArgs e)
        {

            PagedCollectionView pcv = dataGrid1.ItemsSource as PagedCollectionView;
            try
            {
                foreach (CollectionViewGroup group in pcv.Groups)
                {
                    dataGrid1.ExpandRowGroup(group, true);
                }
            }
            catch (Exception ex)
            {
                // Could not expand group.
                MessageBox.Show(ex.Message);
            }

        }
        private void Button_Click_Collapse(object sender, RoutedEventArgs e)
        {

            PagedCollectionView pcv = dataGrid1.ItemsSource as PagedCollectionView;
            try
            {
                foreach (CollectionViewGroup group in pcv.Groups)
                {
                    dataGrid1.ScrollIntoView(group, null);
                    dataGrid1.CollapseRowGroup(group, true);
                }
            }
            catch (Exception ex)
            {
                // Could not collapse group.
                MessageBox.Show(ex.Message);
            }

        }
        private void TextBox_TextChanged(object sender, TextChangedEventArgs e)
        {
            FindTextStr = oFindText.Text.Trim().ToLower();
            SetFilter();
        }
        #endregion

        #region Data
        Exception Invalidate()
        {
            Exception ret = null;
            dataGrid1.ItemsSource = null;
            dataGrid1.IsReadOnly = true;
            dataGrid1.SelectionMode = DataGridSelectionMode.Single;

            if (_SPUSersList_Tmp != null)
                _SPUSersList_Tmp.Clear();
            if (_SPUSersList != null)
                _SPUSersList.Clear();
            _SPUSersList_Tmp = null;
            _SPUSersList_Tmp = new ObjModel.ObservableCollection<SPUser>();
            _SPUSersList = null;
            _SPUSersList = new ObjModel.ObservableCollection<SPUser>();

            if (App._SPUSersList == null)
                return ret;

            foreach (SPUser u in App._SPUSersList)
            {
                if (u.Офис != "Румянцево")
                {
                    _SPUSersList_Tmp.Add(u);
                    _SPUSersList.Add(u);
                }
            }

            myListView = new PagedCollectionView(_SPUSersList);
            dataGrid1.ItemsSource = myListView;
            ApplyGridStyles();

            return ret;
        }
        void SetFilter()
        {
            dataGrid1.ItemsSource = null;
            _SPUSersList.Clear();
            _SPUSersList = null;
            _SPUSersList = new ObjModel.ObservableCollection<SPUser>();
            FindTextStr = FindTextStr.ToLower();
            string f = FindTextStr.Trim();
            foreach (SPUser u in _SPUSersList_Tmp)
            {
                string fio = u.ФИО.ToLower();
                string tel = (u.Тел ?? "").ToLower();
                string office = (u.Офис ?? "").ToLower();
                string dep = (u.Отдел ?? "").ToLower();
                string email = (u.EMail ?? "").ToLower();
                string staff = (u.Должность ?? "").ToLower();
                string acc = (u.AccountName ?? "").ToLower();
                if (String.IsNullOrEmpty(FindTextStr))
                    _SPUSersList.Add(u);
                else if (fio.IndexOf(f) >= 0)
                    _SPUSersList.Add(u);
                else if (tel.IndexOf(f) >= 0)
                    _SPUSersList.Add(u);
                else if (office.IndexOf(f) >= 0)
                    _SPUSersList.Add(u);
                else if (dep.IndexOf(f) >= 0)
                    _SPUSersList.Add(u);
                else if (email.IndexOf(f) >= 0)
                    _SPUSersList.Add(u);
                else if (staff.IndexOf(f) >= 0)
                    _SPUSersList.Add(u);
                else if (acc.IndexOf(f) >= 0)
                    _SPUSersList.Add(u);
            }
            myListView = new PagedCollectionView(_SPUSersList);
            dataGrid1.ItemsSource = myListView;
            ApplyGridStyles();
        }
        #endregion

        private void oTest_Click(object sender, RoutedEventArgs e)
        {
            oDlg.Show();
        }

    }
}
